package tw.cating.karmasaver.rss;

import java.util.ArrayList;

import tw.cating.karmasaver.R;
import tw.cating.karmasaver.intef.selectedChangeListener;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class rssSiteAdapter extends BaseAdapter{
	public class rssSite{
		public String url = null;
		public String title = null;
		public boolean isChecked = false;
	}
	
	ArrayList<rssSite> mList = new ArrayList<rssSite>();
	Context mContext = null;
	
	protected final static String siteCount = "rssSite_Count";
	protected final static String sitePrefix = "rssSite_";
//	protected final static String defaultSite = "http://rss.ptt.cc/StupidClown.xml";
	protected final static String defaultSite = "http://tw.nextmedia.dynamic.feedsportal.com/pf/589007/tw.nextmedia.com/rss/newcreate/kind/rnews/type/";
	selectedChangeListener l = null;
	protected final rssHandler mHandler =  new rssHandler();
	
	public rssSiteAdapter(Context context) {
		mContext = context;
		
		reflash();
	}
	
	public void reflash() {
		mList.clear();
		
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
		int count = sp.getInt(siteCount, 0);
		if (count == 0) {
			rssSite site = new rssSite();
			site.url = new String(defaultSite);
			mList.add(site);
		} else {
			for (int i = 0; i < count; i++) {
				rssSite site = new rssSite();
				site.url = sp.getString(sitePrefix + i, defaultSite);
				mList.add(site);
			}
		}
		
		for (int i = 0; i < mList.size(); i++) {
			mHandler.parserUrl(mList.get(i));
		}		
	}
	
	public void deleteCheckedItem() {
		for (int i = mList.size(); i > 0; i--) {
			rssSite site = mList.get(i - 1);
			if (site.isChecked)
				mList.remove(site);
		}
		if (l != null)
			l.onChanged();
		
		this.notifyDataSetChanged();
	}
	
	public void add(String url) {
		rssSite site = new rssSite();
		site.url = url;
		mHandler.parserUrl(site);
		mList.add(site);
	}
	
	public void commitSite() {
		SharedPreferences.Editor sp = PreferenceManager.getDefaultSharedPreferences(mContext).edit();
		sp.putInt(siteCount, mList.size());
		for (int i = 0; i < mList.size(); i++) {
			rssSite site = mList.get(i);
			sp.putString(sitePrefix + i, site.url);
		}
		sp.commit();
	}
	
	public void setSelectedChangeListener(selectedChangeListener listener) {
		l = listener;
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return mList == null ? 0 : mList.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList == null ? null : mList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final rssSite site = (rssSite)getItem(position);
		if (convertView == null) {
			convertView = ((Activity)mContext).getLayoutInflater().inflate(R.layout.listitem, null);
		}
		TextView text = (TextView)convertView.findViewById(R.id.rssdesc);
		text.setText(site.title);
		TextView rssFrom = (TextView)convertView.findViewById(R.id.rssfrom);
		rssFrom.setText(site.url);
		CheckBox btn = (CheckBox)convertView.findViewById(R.id.checked);
		btn.setVisibility(View.VISIBLE);
		btn.setChecked(site.isChecked);
		btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				site.isChecked = isChecked;
				if (l != null) {
					l.onChanged();
				}
			}
		});
		return convertView;
	}
}
